package stu.sup.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import stu.sup.entity.Coupon;
import stu.sup.entity.Merchants;
import stu.sup.mapper.CouponMapper;
import stu.sup.mapper.MerchantsMapper;
import stu.sup.service.ICouponService;

import java.util.Calendar;
import java.util.Date;

/**
 * Created by HUAWEI on 2020/6/26.
 */
@Service
@Transactional
public class CouponServiceImpl implements ICouponService {
    @Autowired
    CouponMapper couponMapper;
    @Autowired
    MerchantsMapper merchantsMapper;
    @Override
    public boolean addCoupon(Coupon coupon) {
        int count=0;
        count=couponMapper.insert(coupon);
        return 0==count?false:true;
    }

    @Override
    public boolean delateCoupon(int couponId) {
        int count=0;
        count=couponMapper.deleteByPrimaryKey(couponId);
        return 0==count?false:true;
    }

    @Override
    public int updataCoupon(Coupon coupon) {
        int count=couponMapper.updateByPrimaryKeySelective(coupon);
        return count;
    }

    @Override
    public Coupon findCoupon(int couponId) {
        Coupon coupon=new Coupon();
        coupon=couponMapper.selectByPrimaryKey(couponId);
        return coupon;
    }

    @Override
    public boolean Orelective(Coupon coupon) {
        Calendar date=Calendar.getInstance();
        date.setTime(new Date());

        Calendar begin =Calendar.getInstance();
        begin.setTime(coupon.getIssueTime());

        Calendar end=Calendar.getInstance();
        end.setTime(coupon.getOverTime());
        return date.after(begin)&&date.before(end);
    }

    @Override
    public Merchants findmerchant(Coupon coupon) {
        Merchants merchants=merchantsMapper.selectByPrimaryKey(coupon.getCouponNo());
        return merchants;
    }
}
